<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/banner.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <div class="mybanner">
        <ul class="hot">
            <li>
                <a href="#"> <img src="images/banner1.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner2.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner3.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner4.jpg" alt=""> </a>
            </li>
        </ul>
        <div class='arrow'>
            <span class="prev"> < </span>
            <span class="next"> > </span>
        </div>
        <ul class="circle-list">
            <li class="circle active" data-n="0"></li>
            <li class="circle" data-n="1"></li>
            <li class="circle" data-n="2"></li>
            <li class="circle" data-n="3"></li>
        </ul>
    </div>
</body>
<script>
    $(document).ready(function () {
        var $hot = $('.hot')
        var $next = $('.next')
        var $imgWidth = $('img').width()
        var $prev = $('.prev')
        var $arrow = $('.arrow');
        var $mybanner = $('.mybanner')
        var $circles = $('.circle');
        var i = 0;
        var flag = true; //节流阀
        //添加节点
        var $first = $hot.children().first().clone()
        $hot.append($first)

        //添加轮播动画
        //下一页
        $next.on('click',()=>{
                i++;
            $hot.css('transform','translateX('+(-i*$imgWidth)+'px)');
            $hot.css('transition','0.5s');
            flag = true
            if (i == $hot.children().length-1) {
                i=0;
                setTimeout(() => {
                    $hot.css('transform','translateX('+(-i*$imgWidth)+'px)');
                    $hot.css('transition','none');
                    flag = true
                },500)
            }
            setCircles()
        })
        //上一页
            $prev.on('click',()=>{
                flag = false
                if (i == 0) {
                i = $hot.children().length-1;
                    $hot.css('transform','translateX('+(-i*$imgWidth)+'px)');
                    $hot.css('transition','none');
            }
            i--;
            setTimeout(() =>{
                $hot.css('transform','translateX('+(-i*$imgWidth)+'px)');
                $hot.css('transition','0.5s');
                flag = true
            },0)
            setCircles()
        })
        //定时轮播
        var timer = setInterval(()=>{
            $next.click();
        },2000)
        //鼠标移入时
        $mybanner.on('mouseenter',()=>{
            $arrow.show();
            clearInterval(timer);
            timer = null;
        })
        //鼠标移出时
        $mybanner.on('mouseleave',()=>{
            $arrow.hide();
            timer = setInterval(()=>{
            $next.click();
        },2000)
        })
        //设置小圆点
        var setCircles = ()=>{
            $circles.each(function() {
                var n = $(this).data('n');
                if (n == i) {
                    $(this).addClass('active')
                }else{
                    $(this).removeClass('active');
                }
            })
        }

    // 添加小圆点的点击事件
        $circles.on('click', function() {
        i = $(this).data('n'); // 获取点击的小圆点的索引
        $hot.css('transform', 'translateX(' + (-i * $imgWidth) + 'px)');
        $hot.css('transition', '0.5s');
        setCircles(); // 更新小圆点状态
    });
    })

</script>
</html>